<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <!-- 用了一个行列布局 显示公司信息-->
        <!-- <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col>
            <span>江苏传智播客教育科技股份有限公司</span>

          </el-col>
        </el-row> -->
        <treeEl
          :tree-node="company"
          @addDepartment="addDepartment"
        />
        <hr>
        <el-tree
          :data="depts"
          :props="{label: 'name'}"
          default-expand-all
        >
          <!-- <template slot-scope="obj"> -->
          <template #default="obj">
            <treeEl
              :tree-node="obj.data"
              @delDepartment="getDepartment"
              @addDepartment="addDepartment"
              @editDepartment="editDepartment"
            />

          </template>

        </el-tree>
      </el-card>
      <add-dept
        ref="addDept"
        :i-sbool="iSbool"
        :tree-node="treeNode"
        @addDepartment="getDepartment"
        @closeDialog="iSbool=false"
      />
    </div>
  </div>
</template>

<script>
import { getDepartment } from '@/api/company'
import { listToTreeData } from '@/utils/index'
import addDept from '@/views/components/add-dept.vue'
import treeEl from '@/views/components/tree-el.vue'
export default {
  components: {
    treeEl,
    addDept
  },
  data() {
    return {
      treeNode: {},
      // 控制弹窗的变量
      iSbool: false,
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '小王',
        id: ''
      },

      depts: [

      ]
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      // 解构对象,使只直接获取到部门列表
      const { depts } = await getDepartment()
      // 再把请求回来的数据赋值给,this.depts空数组,使其能把页面渲染出来
      this.depts = listToTreeData(depts, '')
      console.log(depts)
    },
    // 点击弹出添加部门框
    addDepartment(treeNode) {
      this.iSbool = true
      this.treeNode = treeNode
    },
    async editDepartment(treeNode) {
      await this.$refs.addDept.getDepartment(treeNode.id)
      this.treeNode = treeNode
      this.iSbool = true
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
